import subprocess
import csv
from PySide import QtCore, QtGui

class BaseTab(QtGui.QWidget):
    def __init__(self, parent = None):
        super(BaseTab, self).__init__(parent)

    refresh = QtCore.Signal()

    def refresh_gui(self):
        raise NotImplementedError("Abstract class method")

    def get_users(self):
        users = {}
        csv.register_dialect('unixpwd', delimiter = ':',
                             quoting = csv.QUOTE_NONE)
        with open('/etc/passwd', 'rb') as f:
            reader = csv.reader(f, 'unixpwd')
            try:
                for row in reader:
                    #we not very truly assume that human users have id>1000
                    if int(row[2]) >= 1000:
                        #map - user:(home_dir, shell)
                        users[row[0]] = (row[5], row[6])
            except csv.Error as e:
                print e
        return users

    def show_message(self, message):
        msgBox = QtGui.QMessageBox()
        msgBox.setText(message)
        msgBox.setWindowTitle("Info")
        msgBox.exec_()

    def execute(self, command):
        return subprocess.Popen(command, shell = True, stdout = subprocess.PIPE,
                                stderr = subprocess.STDOUT).stdout.read()
